<?php
/**
 * Set configuration for database
 * NAME OF CONFIGURATION VARIABLES
 * 
 * [conf_database]
 * rdbms = mysql|sqlite|firebird|mssql|sybase|pgsql|dblib
 * host  = xxx
 * port  = xxx
 * user  = xxx
 * pass  = xxx
 * database = xxx
 * socket = /temp/mysql.sock
 * 
 * @package Nucleo
 * @subpackage Nucleo.Configuration
 * @author Renan Henrique Abreu <renanhabreu@gmail.com>
 * @copyright 2009-2010 Renan Abreu
 */	
abstract class Nucleo_Configuration_Database implements Nucleo_Configuration{
	static private $configured = false;
	
	static public function setConfiguration($fileName){
	
	
		if(!self::$configured){
			if($configuration = parse_ini_file($fileName,TRUE)){
				$DB_LIST = $configuration['conf_database'];
					
				if(isset($DB_LIST)){					
		 			$rdbms    = strtolower($DB_LIST['rdbms']);
		 			$host     = $DB_LIST['host'];
		 			$port     = !empty($DB_LIST['port'])? ';port='.$DB_LIST['port']:'';
		 			$user     = $DB_LIST['user'];
		 			$pass     = $DB_LIST['pass'];
		 			$database = $DB_LIST['database'];
		 			$sock     = !empty($DB_LIST['socket'])? ';unix_socket='.$DB_LIST['socket']:'';
		 			
		 			
		 			switch($rdbms){
		 				case 'mysql':
		 					$dns = $rdbms.':host='.$host.$port.$sock.';dbname='.$database;
		 					break;
		 				case 'mssql';
		 				case 'dblib';
		 				case 'sybase':
		 					$dns = $rdbms.':host='.$host.$port.';dbname='.$database;
		 					break;
		 				case 'sqlite':
		 					$dns = $rdbms.':'.$host.'/'.$database;
		 					break;
		 				case 'firebird':
		 					$dns = $rdbms.':User='.$user.';Password='.$pass
		 						 .';Database='.$database.';DataSource='.$host.';Port='.$port;
		 					break;
		 				case 'pgsql':
		 					$dns = $rdbms.':dbname='.$database.';user='.$user
		 						 . ';password='.$pass.';host='.$host.';port='.$port;
		 					break;
		 				default:
		 					$dns = null;
		 					throw new Nucleo_Exception_Sifra('002',true);
		 					break;
		 			}
		 			
		 			define('DNS',$dns);
		 			define('USER',$user);
		 			define('PASS',$pass);
		 					 														
				}else
					throw new Nucleo_Exception_Sifra('014',true);
				self::$configured = true;		
			}else{
				throw new Nucleo_Exception_Load('003');
			}
		}
	}
} 
?>